package app;

import datastructure.Instance;
import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//import org.jfree.ui.RectangleEdge;

public class LineChart extends JFrame {

    int divide = 0;
    int numofCustomer = 0;
    int numofPFC = 0;
    int numofTask = 0;
    double a = 0; // platoon saving factor
    double b = 0;// platoon saving factor
    double Len = 0; // maximum platoon length
    double c1 = 0; // cost driver
    double c2 = 0; //cost fuel
    double miu = 0; //cost fuel
    double Fi = 0;
    double Fm = 0;
    double[] delta;
    double[][] distanceMatrix;
    int[][] ODdemand;
    double[][] coordinateofPFC;
    double[][] coordinateofCustomer;
    double[][] coordinate;
    int experimentnumber = 0;
    String time;

    double[] graph;
    int numofiter;

    public LineChart(Instance instance) {
        this.numofPFC = instance.numofPFC;
        this.divide = instance.divide;
        this.numofCustomer = instance.numofCustomer;
        this.numofTask = instance.numofTask;
        this.a = instance.a;// platoon saving factor
        this.b = instance.b; // platoon saving factor
        this.Len = instance.Len; // maximum platoon length
        this.c1 = instance.c1; // cost weight
        this.c2 = instance.c2;
        this.miu = instance.miu;
        this.Fi = instance.Fi; // cost weight
        this.Fm = instance.Fm; // cost weight
        this.delta = instance.delta;
        this.ODdemand = instance.ODdemand;
        this.distanceMatrix = instance.distanceMatrix;
        this.coordinateofPFC = instance.coordinateofPFC;
        this.coordinateofCustomer = instance.coordinateofCustomer;
        this.experimentnumber = experimentnumber;
        this.time = time;
    }

    public static CategoryDataset GetDataset(double[] graphs, int numofiters, int divides) {

        DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
//        System.out.println("start--------------------------convergence graph");
        for (int i = 0; i < numofiters; i++) {
            mDataset.addValue(graphs[i], "n = " + divides, String.valueOf(1 + i));
//            System.out.println(graphs[i]);
        }
//        System.out.println("end--------------------------convergence graph");
        return mDataset;
    }

    public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) {
        FileOutputStream out = null;
        try {
            File outFile = new File(outputPath);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            out = new FileOutputStream(outputPath);
            ChartUtilities.writeChartAsPNG(out, chart, 600, 400);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
        }
    }

    public void TimeSeriesChart(double[] data, int iter) {
        this.graph = data;
        this.numofiter = iter;

        StandardChartTheme mChartTheme = new StandardChartTheme("CN");
        mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20));
        mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15));
        mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
        ChartFactory.setChartTheme(mChartTheme);

        CategoryDataset mDataset = GetDataset(graph, numofiter, divide);

        JFreeChart mChart = ChartFactory.createLineChart(
                "",
                "",
                "objective value",
                mDataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);

        mChart.getLegend().setVisible(true);

        CategoryPlot mPlot = (CategoryPlot) mChart.getPlot();
        mPlot.setBackgroundPaint(Color.white);
        mPlot.setOutlinePaint(Color.BLACK);

        NumberAxis numberaxis = (NumberAxis) mPlot.getRangeAxis();
        numberaxis.setRangeWithMargins(graph[numofiter - 1] - 10, graph[0] + 10);

        LineAndShapeRenderer renderer = (LineAndShapeRenderer) mPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.red);

        CategoryAxis domainAxis = mPlot.getDomainAxis();
        for (int i = 0; i < numofiter; i++) {
            if (i % (numofiter / 1) == 0) {
                domainAxis.setTickLabelPaint(Integer.toString(i), Color.black);
            } else {
                domainAxis.setTickLabelPaint(Integer.toString(i), Color.white);
            }
        }
        domainAxis.setLowerMargin(-0.08);
        domainAxis.setLabelFont(new Font("宋书", Font.PLAIN, 15));
        domainAxis.setTickLabelFont(new Font("宋书", Font.PLAIN, 15));
        domainAxis.setLowerMargin(0.01);
        domainAxis.setUpperMargin(0.06);
        domainAxis.setMaximumCategoryLabelLines(10);
        domainAxis.setMaximumCategoryLabelWidthRatio(500);

        ChartFrame mChartFrame = new ChartFrame("折线图", mChart);
        mChartFrame.pack();
        mChartFrame.setVisible(true);

//		saveAsFile(mChart, "model/LineChart model/"+ divide +".png", 600, 400);
    }

}
